<?php

namespace App\Http\Controllers\Home;

use App\Model\Cate;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CommonController extends Controller
{
    //
    public function __construct()
    {
        //获取所有分类
        $cate = Cate::get();

        //存放一级变量
        $cateone = [];

        //存放二级变量
        $catetwo = [];

        foreach ($cate as $k=>$v) {
            //取出所有一级类存放到$cateone
            if($v->cate_pid==0){
                $cateone[$k]=$v;
                //取出当前一级类下的二级类
                foreach ($cate as $m=>$n){
                    if($v->cate_id==$n->cate_pid){
                        $catetwo[$k][$m]=$n;
                    }
                }
            }
        }
        //dd($catetwo);

        view()->share('cateone',$cateone);
        view()->share('catetwo',$catetwo);
    }
}
